/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.pay;

import cn.hutool.core.util.ObjectUtil;
import com.lxk.oms.order.domain.req.PayOrderReq;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.oms.order.service.IOmsOrderService;
import com.lxk.service.core.Result;
import com.lxk.service.ums.api.UmsServiceApi;
import com.lxk.service.ums.response.UmsUserDto;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
import lombok.AllArgsConstructor;
import org.dromara.common.core.exception.ServiceException;

import java.util.List;

/**
 * 填充支付链路参数
 *
 * @author heyi
 * 2024/8/22
 */
@LiteflowComponent(value = FlowPayOrderNodeConst.PAY_FILL_CONTEXT)
@AllArgsConstructor
public class PayFillContextNode extends NodeComponent {
    private final IOmsOrderService iOmsOrderService;
    private final UmsServiceApi umsServiceApi;
    private final LFLog log = LFLoggerManager.getLogger(FlowExecutor.class);

    @Override
    public void process() throws Exception {
        PayOrderReq req = getRequestData();
        PayContext contextBean = getContextBean(PayContext.class);
        log.info("支付订单参数:{}", req);
        List<String> orderNoList = req.getOrderNo();
        Long userId = req.getUserId();
        List<OmsOrderVo> omsOrderVo = iOmsOrderService.queryByOrderSnList(orderNoList, userId);
        if (ObjectUtil.isNull(omsOrderVo)) {
            throw new ServiceException("订单不存在");
        }
        contextBean.setOmsOrder(omsOrderVo);
        Result<UmsUserDto> umsUserDtoResult = umsServiceApi.queryById(userId);
        if (umsUserDtoResult.isFail()) {
            throw new ServiceException(umsUserDtoResult.getMsg());
        }
        UmsUserDto user = umsUserDtoResult.getData();
        contextBean.setUser(user);
        contextBean.setPayResult(new PayContext.PayResult());
        contextBean.setPayType(req.getPayType());
        contextBean.setOrderNo(orderNoList);
        contextBean.setUserId(req.getUserId());
        contextBean.setPayInnerOrder(new PayContext.PayInnerOrder());
    }
}
